import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/picker/imagePicker/image_picker.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class MultipleUploadImage extends StatefulWidget {
  final int maxCount;
  final List<String> images;
  final ValueChanged<List<String>> onChanged;

  const MultipleUploadImage({
    Key key,
    this.maxCount = 3,
    this.images,
    this.onChanged,
  }) : super(key: key);

  @override
  _MultipleUploadImageState createState() => _MultipleUploadImageState();
}

class _MultipleUploadImageState extends State<MultipleUploadImage> {
  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];

    widget.images?.forEach((item) {
      children.add(BaseImage(
        item,
        width: 120.w,
        height: 120.w,
        borderRadius: BorderRadius.circular(
          10.w,
        ),
      ));
    });

    if (widget.images.length < widget.maxCount) {
      children.add(GestureDetector(
        onTap: () async {
          var image = await ImagePicker.pickImage(context);
          if (image != null) {
            var url = await UploadUtil.uploadImage(image);
            if (url != null && widget.onChanged != null) {
              widget.onChanged([...widget.images, url]);
            }
          }
        },
        child: BaseImage(
          'assets/image/upload_image_bg.png',
          width: 120.w,
          height: 120.w,
        ),
      ));
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(
            top: 30.w,
            bottom: 20.w,
          ),
          child: Text(
            '添加图片，最多上传${widget.maxCount}张',
            style: TextStyleConstant().normal_4_26,
          ),
        ),
        Container(
          child: Wrap(
            spacing: 20.w,
            runSpacing: 20.w,
            children: children,
          ),
        ),
      ],
    );
  }
}
